package handlers

import (
	"github.com/rs/xid"
	"github.com/shugen/models"
	"github.com/shugen/utils"
	"net/http"
	"strconv"
	"time"
)

//insert 门宫关系
func (c *Controller) IMengong(w http.ResponseWriter, r *http.Request) {

	if r.Method != "POST" {
		return
	}

	type_value, _ := strconv.Atoi(r.PostFormValue("type"))
	mengong := &models.MenGong{
		MenGongId: []byte(xid.New().String()),
		Type:      type_value,
		Title:     r.PostFormValue("title"),
		Desc:      r.PostFormValue("desc"),
		Status:    1,
		CreatedAt: time.Now().UTC(),
		UpdatedAt: time.Now().UTC(),
	}
	if err := c.Svc.InsertMenGong(mengong); err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mengong))
}

//update 门宫关系
func (c *Controller) UMengong(w http.ResponseWriter, r *http.Request) {

	mg := &models.MenGong{
		MenGongId: []byte(r.PostFormValue("MenGongId")),
		//Type:      0,
		Title:  r.PostFormValue("title"),
		Desc:   r.PostFormValue("desc"),
		Status: 0,
		//CreatedAt: time.Time{},
		UpdatedAt: time.Now().UTC(),
	}

	mengong, err := c.Svc.UpdateMenGong(mg)

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mengong))
}

//select 门宫关系
func (c *Controller) SMengong(w http.ResponseWriter, r *http.Request) {

	mg, err := c.Svc.SelectMenGong(&models.MenGong{MenGongId: []byte(r.PostFormValue("MenGongId"))})

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mg))
}

//select List 门宫关系
func (c *Controller) LMengong(w http.ResponseWriter, r *http.Request) {

	mgs, err := c.Svc.ListMenGong()

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mgs))
}
